package com.ywh.log;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;

/**
 * 日志分析工具
 * 
 * @author yaowenhao
 * @since 2011-4-7
 * @version 1.0
 */
public class LogToolForGamble {

	public static HashSet<String> items = new HashSet<String>();
	public static HashSet<String> names = new HashSet<String>();

	private void init() {
		try {
			FileInputStream fis = new FileInputStream("item.txt");
			BufferedReader br = new BufferedReader(new InputStreamReader(fis));
			String line = null;
			while ((line = br.readLine()) != null) {
				items.add(line.trim());
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		// System.out.println(items);
		System.out.println("初始化物品成功");
	}

	/**
	 * 寄卖日志
	 * 
	 * @param logFile
	 * @param minId
	 */
	public void getLostConsignItems(String logFile, PrintWriter pw) {
		BufferedReader br = null;
		try {
			br = new BufferedReader(new FileReader(logFile));
			String line = null;
			while ((line = br.readLine()) != null) {
				int start = line.indexOf("|");
				int end = line.indexOf("|", start + 1);
				if (start != -1 && end != -1) {
					String mStr = line.substring(start + 1, end);
					// System.out.println(mStr);
					if (items.contains(mStr)) {
						// System.out.println(line);
						pw.println(line);
						int startP = line.indexOf("玩家【");
						int endP = line.indexOf("】", startP);
						String name = line.substring(startP + 3, endP);
						names.add(name);
					}
				}
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			try {
				if (br != null)
					br.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		if (args.length < 3) {
			System.out.println("输入正确的参数 ： 月份  最小值  最大值  输出文件名");
			return;
		}
		LogToolForGamble ltf = new LogToolForGamble();
		ltf.init();

		String startname = "log/2011-0";
		int month = Integer.parseInt(args[0]);
		int min = Integer.parseInt(args[1]);
		int max = Integer.parseInt(args[2]);
		String endname = "GambleItem.log";
		String out = args[3];
		PrintWriter pw = null;
		try {
			pw = new PrintWriter(new FileWriter(out, true));
			for (; min <= max; min++) {
				String fullName = null;
				if (min < 10) {
					fullName = startname + month + "-" + "0" + min + endname;
				} else {
					fullName = startname + month + "-" + min + endname;
				}
				System.out.println("开始分析" + fullName);
				ltf.getLostConsignItems(fullName, pw);
			}
			System.out.println("非法玩家:" + names);
			pw.println(names.toString());
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (pw != null)
				pw.close();
		}
		System.out.println("结果已输出到文件：" + out);
		System.out.println("分析结束");
	}

}
